package cn.lblbc.lib.view

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import cn.lblbc.lib.utils.dp2Px
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class HorizontalImageViews(context: Context, attrs: AttributeSet?) : NestedHorizontalScrollView(context, attrs) {

    var imagesLayout: LinearLayout

    init {
        imagesLayout = LinearLayout(context, null)
        imagesLayout.orientation = LinearLayout.HORIZONTAL
        addView(imagesLayout)
    }

    fun setData(imageUrls: List<String>, imageWidth: Float) {
        imageUrls.filter { it.isNotEmpty() }.forEach {
            addImageView(it, imageWidth)
        }
    }

    private fun addImageView(imageUrl: String, imageWidth: Float) {
        val imageView = ImageView(context)
        val lp = LinearLayout.LayoutParams(dp2Px(context, imageWidth), ViewGroup.LayoutParams.MATCH_PARENT)
        lp.leftMargin = dp2Px(context, 5f)
        lp.rightMargin = dp2Px(context, 5f)
        imageView.layoutParams = lp
        Glide.with(context).load(imageUrl).apply(RequestOptions.bitmapTransform(RoundedCorners(dp2Px(context, 20f)))).into(imageView);
        imagesLayout.addView(imageView)
    }
}